package com.waa.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.waa.gulimall.common.utils.R;
import com.waa.gulimall.ware.feign.MemberFeignService;
import com.waa.gulimall.ware.vo.FareVo;
import com.waa.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waa.gulimall.common.utils.PageUtils;
import com.waa.gulimall.common.utils.Query;

import com.waa.gulimall.ware.dao.WareInfoDao;
import com.waa.gulimall.ware.entity.WareInfoEntity;
import com.waa.gulimall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        final QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        if (key!=null) {
            wrapper.eq("id", key).or().like("name", key).or().like("address", key).or().like("areacode", key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        final FareVo fareVo = new FareVo();
        final R info = memberFeignService.info(addrId);//收货地址信息
        final MemberAddressVo infoData = (MemberAddressVo) info.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
        });
        if (infoData!=null){
            final String phone = infoData.getPhone();
            final String fare = phone.substring(phone.length() - 1, phone.length());
            fareVo.setFare(new BigDecimal(fare));
            fareVo.setAddress(infoData);
            return fareVo;
        }

        return null;
    }

}